--- 模块功能：SC7A20 士兰微G-senser
-- @module SC7A20
-- @author ghd

--require "log"
local sys = require("sys")
--local bit = require("bit")
--require "misc"

local gsenser = {}
local d1Name = "gsenser"

-- i2c ID
local i2cid = 0

local SC7A20_ADDRESS = 0x19 --7bit地址
--SDO悬空(address = 0x19,wt=0x32,rd=0x33) SDO=0 (address = 0x18,wt=0x30,rd=0x31)
local SC7A20_WT = 0x32
local SC7A20_RD = 0x33

---SC7A20 所用寄存器地址
local SC7A20_OUT_TEMP_L  = 0x0C
local SC7A20_OUT_TEMP_H  = 0x0D
local SC7A20_WHO_AM_I    = 0x0F
local SC7A20_TEMP_CFG    = 0x1F
local SC7A20_CTRL_REG1   = 0x20
local SC7A20_CTRL_REG2   = 0x21
local SC7A20_CTRL_REG3   = 0x22
local SC7A20_CTRL_REG4   = 0x23
local SC7A20_CTRL_REG5   = 0x24
local SC7A20_CTRL_REG6   = 0x25
local SC7A20_REFERENCE   = 0x26
local SC7A20_STATUS_REG  = 0x27

local SC7A20_XOUT_L     =   0x28
local SC7A20_XOUT_H     =   0x29
local SC7A20_YOUT_L     =   0x2A
local SC7A20_YOUT_H     =   0x2B
local SC7A20_ZOUT_L     =   0x2C
local SC7A20_ZOUT_H     =   0x2D


-- 初始化
function SC7A20_init()
    log.info("SC7A20_init", i2cid)
    if i2c.setup(i2cid,i2c.SLOW) == 0 then
        log.error("SC7A20","i2c.setup fail")
        return false
    end
    sys.wait(2000)
    --i2c.send(i2cid, SC7A20_ADDRESS, {SC7A20_CTRL_REG1, 0x07})
    log.info("i2c readReg")
    local data = i2c.readReg(i2cid, SC7A20_ADDRESS, SC7A20_CTRL_REG1, 1, 1)
    log.info("i2c data=",pack.unpack(data,"b"))

    --0x17-1Hz 0x27-10hz
    local result = i2c.writeReg(i2cid, SC7A20_ADDRESS, SC7A20_CTRL_REG1, 0x27, 1)
    --log.info("i2c set SC7A20_CTRL_REG1 ", result)
    if result == false then return false end
    
    result = i2c.writeReg(i2cid, SC7A20_ADDRESS, SC7A20_CTRL_REG2, 0x88, 1) --高通滤波模式(正常模式) + 内部滤波
    --log.info("i2c set SC7A20_CTRL_REG2 ", result)
    if result == false then return false end
    
    --数据类型配置
    result = i2c.writeReg(i2cid, SC7A20_ADDRESS, SC7A20_CTRL_REG4, 0x88, 1) --读取后更炫, +/-2G, 低通滤波器使能
    --log.info("i2c set SC7A20_CTRL_REG4 ", result)
    if result == false then return false end
    
    log.info("i2c init_ok")
end


-- SC7A20 TEST --
page=0
local pre_z = 300
local x, y, z
local function SC7A20()    
    if SC7A20_init() == false then 
        return false
    end
    sys.wait(3000)
    x=0
    y=0
    z=0
    while true do
        data = i2c.readReg(i2cid, SC7A20_ADDRESS, 0xA8, 6, 1)
        --log.info("hex=", string.toHex(data))
        index, x, y, z=pack.unpack(data,"<hhh")
        --x = bit.rshift(x,4)
        x = x / 16
        y = y / 16
        z = z / 16
        --log.info("SC7A20", "accel.x", x,"accel.y", y,"accel.z", z)
        if z < 200 and math.abs(z-pre_z)>100 then
            if page == 1 then
                page = 0
            else
                page = 1
            end
            log.info(page, "SC7A20", "accel.x", x,"accel.y", y,"accel.z", z)            
        end
       
        pre_z = z

        sys.wait(100)
    end
end

sys.taskInit(SC7A20)

function gsenser.getPage()
    return page
end

return gsenser
